package edu.nctu.csie.jichang.database.model.cell;

import edu.nctu.csie.jichang.database.model.Converter;
import edu.nctu.csie.jichang.database.util.DBException;

/**
 * ColumnInfo
 * @author Jichang
 */
public class ColumnInfo extends AbstractRootElement<ColumnInfo>{
	
	private int rawType = Integer.MIN_VALUE;
	private String rawTypeName;
	private int size;
	private boolean nullable = true;
	private String rawDefault;
	private ColumnType columnType;
	
	public ColumnInfo(String pColumnName) {
		super(pColumnName);
	}
	
	public ColumnInfo(String pColumnName, int pRawType,String pRawTypeName,int pSize,boolean pNullable,String pRawDefault) {
		super(pColumnName);
		rawType = pRawType;
		rawTypeName = pRawTypeName;
		size = pSize;
		nullable = pNullable;
		rawDefault = pRawDefault;
		columnType = Converter.getColumnType(rawType);
	}
	public ColumnInfo(String pColumnName, ColumnType pColumnType) {
		super(pColumnName);
		columnType = pColumnType;
	}
	public ColumnInfo(String pColumnName, ColumnType pColumnType,boolean pNullable) {
		super(pColumnName);
		columnType = pColumnType;
		nullable = pNullable;
	}
	public int getRawType() {
		if(rawType == Integer.MIN_VALUE){
			throw new DBException("unknow raw type");
		}
		return rawType;
	}
	public void setRawType(int rawType) {
		columnType = Converter.getColumnType(rawType);
		this.rawType = rawType;
	}
	public ColumnType getColumnType() {
		return columnType;
	}
	public void setColumnType(ColumnType columnType) {
		this.columnType = columnType;
	}
	public boolean isNullable() {
		return nullable;
	}
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}
	public String getRawTypeName() {
		return rawTypeName;
	}
	public void setRawTypeName(String rawTypeName) {
		this.rawTypeName = rawTypeName;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public int getSize() {
		return size;
	}
	public void setRawDefault(String rawDefault) {
		this.rawDefault = rawDefault;
	}
	public String getRawDefault() {
		return rawDefault;
	}
	
	@Override
	public String toString() {
		return "[ " + getName() + " => " + columnType + "," + nullable + " ]";
	}

	@Override
	public boolean isSame(ColumnInfo pObj) {
		if (this == pObj)
			return true;
		if (pObj == null)
			return false;
		if (columnType != pObj.columnType)
			return false;
		if (nullable != pObj.nullable)
			return false;
		if (rawDefault == null) {
			if (pObj.rawDefault != null)
				return false;
		} else if (!rawDefault.equals(pObj.rawDefault))
			return false;
		if (rawType != pObj.rawType)
			return false;
		if (rawTypeName == null) {
			if (pObj.rawTypeName != null)
				return false;
		} else if (!rawTypeName.equals(pObj.rawTypeName))
			return false;
		if (size != pObj.size)
			return false;
		return true;
	}
}